package sa.com.sure.ejtimaa.database;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.model.MeetingItemInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class MeetingItems {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "MeetingItems";
	
    public static final String MeetingItemIDField = "MeetingItemID";
    public static final String MeetingIDField = "MeetingID";
    public static final String TitleField = "Title";
    public static final String DescriptionField = "Description";
    
    private String[] allColumns = { MeetingItemIDField, MeetingIDField , TitleField, DescriptionField };
    
    Cursor cursor = null;    
    
    public MeetingItems(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
    
        
    public static MeetingItems Instance = new MeetingItems(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		
		if(cursor!=null && !cursor.isClosed())
		{
			cursor.close();
		}
		dbHelper.close();
		
	}
	
	public void addItem(MeetingItemInfo item) { 
        
		try
		{
		
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(MeetingItemIDField, item.GetMeetingItemID()); 
	        values.put(MeetingIDField, item.GetMeetingID()); 
	        values.put(TitleField, item.GetTitle()); 
	        values.put(DescriptionField, item.GetDescription());
	        
	        dbHelper.db.insert(TABLE_Name, null, values); 
                
		}
		finally{
			
			close();
		}
    } 
	
	
	public MeetingItemInfo GetItem(int MeetingItemID) throws SQLException{
	
		try{
			open();
		
			MeetingItemInfo item= null; 
			
			cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingItemIDField + "=" + MeetingItemID ,null,null,null,null);
	        if (cursor != null ) {	        	
	        	if(cursor.moveToFirst())
	        		item =   getItemInfo(cursor);
	        	
	        }	                
	        
	    	return item;
		}
		finally{
			
			close();
		}	    	
		    
	}
	
	

	 public boolean updateItem(MeetingItemInfo item) {
		 
		 try{
			 
		 	open();
		 	
		    ContentValues values = new ContentValues();		  
		    	        
	        values.put(MeetingIDField, item.GetMeetingID()); 
	        values.put(TitleField, item.GetTitle()); 
	        values.put(DescriptionField, item.GetDescription());
	        
		    boolean res =   dbHelper.db.update(TABLE_Name, values, MeetingItemIDField + "=" + item.GetMeetingItemID() , null) > 0;
		    
		    return res;
		}
		finally{
			
			close();
		}		    
	 }
	
	public List<MeetingItemInfo> GetAllItems() throws SQLException, ParseException{
		
		open();
	
		List<MeetingItemInfo> items = new ArrayList<MeetingItemInfo>();
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, null, null,null,null,null,null);         		
        		 
    		if (cursor.moveToFirst()){
			   do{
				   items.add(getItemInfo(cursor));
			   	 }
			   while(cursor.moveToNext());
    		}
        		        		                    
        
        close();
        
    	return 	items;	        
	    
	}
	 
	
	public List<MeetingItemInfo> GetItemsByMeetingId(int MeetingId) throws SQLException, ParseException{
		
		open();
	
		List<MeetingItemInfo> items = new ArrayList<MeetingItemInfo>();
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingIDField + "=" + MeetingId,null,null,null,null);

        		if (cursor.moveToFirst()){
    			   do{
    				   items.add(getItemInfo(cursor));
    			   	 }
    			   while(cursor.moveToNext());     
        		        		     
        		}	        
        
        close();
        
    	return 	items;	        
	    
	}
	
    private MeetingItemInfo getItemInfo(Cursor cursor) {  
  
    	return new MeetingItemInfo(cursor.getInt(cursor.getColumnIndex(MeetingItemIDField)),
					    			cursor.getInt(cursor.getColumnIndex(MeetingIDField)),
					    			cursor.getString(cursor.getColumnIndex(TitleField)),
					    			cursor.getString(cursor.getColumnIndex(DescriptionField)));    	   
	}
}
